package com.hankol.message.bean;

import android.os.Parcel
import android.os.Parcelable
import android.util.Log
import com.baselibrary.extension.checkValue
import io.rong.common.RLog
import io.rong.imlib.MessageTag
import io.rong.imlib.model.MessageContent
import org.json.JSONException
import org.json.JSONObject
import java.io.UnsupportedEncodingException

/** Created by Beyond on 2016/12/5.  */
@MessageTag(value = "HK:DynamicCard", flag = MessageTag.ISCOUNTED or MessageTag.ISPERSISTED)
class DynamicCardMessage : MessageContent {
    var id: Long = 0
    //动态类型  1：图片 2：视频 3话题、4文章、 5直播、6 百科  7 讲坛的视频详情
    var type: Int = 0
    var cover: String = ""
    var title: String = ""
    var heardUrl: String = ""
    var nickName: String = ""
    var isFans:Int = 0
    var path: String = ""
    //直播状态  0 ：未开始  1：直播中 2：已结束
    // const val LIVE_STATE_NO_START=0
    //const val LIVE_STATE_ING=1
    //const val LIVE_STATE_END=2
    var liveState: Int = 0
    var sendMessage: String = ""

    constructor() {}

    constructor(id: Long = 0, type: Int = 0, cover: String = "", title: String = "", heardUrl: String = "", nickName: String = "", isFans: Int = 0,path: String = "",liveState:Int=0, sendMessage: String = "") {
        this.id = id
        this.type = type
        this.cover = cover
        this.title = title
        this.heardUrl = heardUrl
        this.nickName = nickName
        this.isFans = isFans
        this.path = path
        this.liveState = liveState
        this.sendMessage = sendMessage
    }

    override fun encode(): ByteArray? {
        val jsonObject = JSONObject()

        try {
            // 消息携带用户信息时, 自定义消息需添加下面代码
            if (getJSONUserInfo() != null) {
                jsonObject.putOpt("user", getJSONUserInfo());
            }
            jsonObject.putOpt("id", id)
            jsonObject.putOpt("type", type)
            jsonObject.putOpt("cover", cover)
            jsonObject.putOpt("title", title)
            jsonObject.putOpt("heardUrl", heardUrl)
            jsonObject.putOpt("nickName", nickName)
            jsonObject.putOpt("isFans", isFans)
            jsonObject.putOpt("path", path)
            jsonObject.putOpt("liveState", liveState)
            jsonObject.putOpt("sendMessage", sendMessage)
        } catch (e: Exception) {
        }

        try {
            return jsonObject.toString().toByteArray(charset("UTF-8"))
        } catch (e: UnsupportedEncodingException) {
            e.printStackTrace()
        }

        return null
    }

    constructor(data: ByteArray) {
        if (data == null) {
            RLog.e(TAG, "data is null ");
            return;
        }
        var jsonStr: String? = null
        try {
            jsonStr = String(data, Charsets.UTF_8)
        } catch (e: UnsupportedEncodingException) {
            e.printStackTrace()
        }

        try {
            val jsonObj = JSONObject(jsonStr)
            // 消息携带用户信息时, 自定义消息需添加下面代码
            if (jsonObj.has("user")) {
                setUserInfo(parseJsonToUserInfo(jsonObj.getJSONObject("user")));
            }

            if (jsonObj.has("id")) id = jsonObj.optLong("id")
            if (jsonObj.has("type")) type = jsonObj.optInt("type")
            if (jsonObj.has("cover")) cover = jsonObj.optString("cover")
            if (jsonObj.has("title")) title = jsonObj.optString("title")
            if (jsonObj.has("heardUrl")) heardUrl = jsonObj.optString("heardUrl")
            if (jsonObj.has("nickName")) nickName = jsonObj.optString("nickName")
            if (jsonObj.has("isFans")) isFans = jsonObj.optInt("isFans")
            if (jsonObj.has("path")) path = jsonObj.optString("path")
            if (jsonObj.has("liveState")) liveState = jsonObj.optInt("liveState")
            if (jsonObj.has("sendMessage")) sendMessage = jsonObj.optString("sendMessage")
        } catch (e: JSONException) {
            RLog.e(TAG, "JSONException " + e.message)
        }

    }

    constructor(`in`: Parcel) {
        id = `in`.readLong()
        type = `in`.readInt()
        cover = `in`.readString().checkValue()
        title = `in`.readString().checkValue()
        heardUrl = `in`.readString().checkValue()
        nickName = `in`.readString().checkValue()
        isFans= `in`.readInt()
        path = `in`.readString().checkValue()
        liveState = `in`.readInt()
        sendMessage = `in`.readString().checkValue()
    }

    override fun describeContents(): Int {
        return 0
    }

    override fun writeToParcel(dest: Parcel, flags: Int) {
        dest.writeLong(id)
        dest.writeInt(type)
        dest.writeString(cover)
        dest.writeString(title)
        dest.writeString(heardUrl)
        dest.writeString(nickName)
        dest.writeInt(isFans)
        dest.writeString(path)
        dest.writeInt(liveState)
        dest.writeString(sendMessage)
    }


    companion object {
        private val TAG = "DynamicCardMessage"

        fun obtain(id: Long = 0, type: Int = 0, cover: String = "", title: String = "", heardUrl: String = "", nickName: String = "", isFans: Int = 0, path: String = "", liveState: Int = 0, sendMessage: String = ""): DynamicCardMessage {
            return DynamicCardMessage(id, type, cover, title, heardUrl, nickName,isFans,path,liveState, sendMessage)
        }

        @JvmField
        val CREATOR: Parcelable.Creator<DynamicCardMessage> = object : Parcelable.Creator<DynamicCardMessage> {
            override fun createFromParcel(source: Parcel): DynamicCardMessage {
                return DynamicCardMessage(source)
            }

            override fun newArray(size: Int): Array<DynamicCardMessage?> {
                return arrayOfNulls(size)
            }
        }
    }
}
